const TokenKey = "Admin-LandManagement-Token";
const CodeKey = "Admin-LandManagement-Code";
export function getToken(): string | null {
	return localStorage.getItem(TokenKey);
}

export function setToken(token) {
	return localStorage.setItem(TokenKey, token);
}

// 获取门户的token(修改信息查询信息)
export function getPortalManageToken(): string | null {
	return localStorage.getItem("Admin-PortalManage-Token");
}

// 移除门户的token、code
export function clearPortalManageToken() {
	localStorage.removeItem("Admin-PortalManage-Token");
	localStorage.removeItem("Admin-PortalManage-Code");
}

export function removeToken() {
	return localStorage.removeItem(TokenKey);
}

export function getCode(): string | null {
	return localStorage.getItem(CodeKey);
}

export function setCode(code) {
	return localStorage.setItem(CodeKey, code);
}

export function removeCode() {
	return localStorage.removeItem(CodeKey);
}

// 随机生成id
export function createUuid() {
	return Math.random().toString(36).slice(2);
}

// 翻译字典值
export function dictName(key, list) {
	const item = list.find((item) => {
		return item["value"] === key || item["key"] === key;
	});
	return item ? item["label"] || item["value"] : "-";
}

// 判定输入处是否禁用
export function disabled(value) {
	if (value === undefined || value === "" || value === null) {
		return false;
	}
	return true;
}

export function getQueryString(urlString, name) {
	const url = new URL(urlString);
	return url.searchParams.get(name) || null;
}

// 删除指定参数
export function removeURLParameter(url: string, parameter: string) {
	const urlparts = url.split("?");
	if (urlparts.length >= 2) {
		// 参数名前缀
		const prefix = encodeURIComponent(parameter) + "=";
		const pars = urlparts[1].split(/[&;]/g);

		// 循环查找匹配参数
		for (let i = pars.length; i-- > 0; ) {
			if (pars[i].lastIndexOf(prefix, 0) !== -1) {
				// 存在则删除
				pars.splice(i, 1);
			}
		}

		return urlparts[0] + (pars.length > 0 ? "?" + pars.join("&") : "");
	}
	return url;
}

// 返回数值输入框小数位
export function getPrecision(x, y) {
	if (x === "" || x === null || x === undefined) {
		return 0;
	}
	const s = x.toString().split(".");
	if (Array.isArray(s) && s.length === 2) {
		if (s[1].length < y) {
			return s[1].length;
		}
		return y;
	}
	return 0;
}

export const getStrFullLength = (str = "") => {
	let len = 0;
	for (let i = 0; i < str.length; i++) {
		const c = str.charCodeAt(i);
		if (c > 255) {
			len += 2;
		} else {
			len += 1;
		}
	}
	return len;
};

export const getStrFull = (str = "", length) => {
	let len = 0;
	let showStr = "";
	for (let i = 0; i < str.length; i++) {
		const c = str.charCodeAt(i);
		if (c > 255) {
			len += 2;
		} else {
			len += 1;
		}
		if (len <= length) {
			showStr += str[i];
		}
	}
	return showStr;
};

export const returnValidStr = (str) => {
	if (str === undefined || str === null || str === "") {
		return "-";
	}
	return str;
};

export const isValid = (str) => {
	if (str === undefined || str === null || str === "") {
		return false;
	}
	return true;
};

// 富文本反转义html
export const escapeHtml = (str) => {
	const arrEntities = { lt: "<", gt: ">", nbsp: " ", amp: "&", quot: '"' };
	return str.replace(/&(lt|gt|nbsp|amp|quot);/gi, function (all, t) {
		return arrEntities[t];
	});
};

// 富文本转义html
export const htmlEscape = (str) => {
	return str.replace(/[<>&"]/g, function (c) {
		return { "<": "&lt;", ">": "&gt;", "&": "&amp;", '"': "&quot;" }[c];
	});
};

// 判断操作系统是不是window
export const getPosType = () => {
	const agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("win32") >= 0 || agent.indexOf("wow32") >= 0) {
		return true;
	}
	if (agent.indexOf("win64") >= 0 || agent.indexOf("wow64") >= 0) {
		return true;
	}
	return false;
};
